package com.cloud.base.thread.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Program: cloud-pay
 * @Description:
 * @Version: v1.0.0
 * @Author: hantianyang
 * Modification History:
 * Date             Author      Version     Description
 * -------------------------------------------------------------
 * 2021/11/26  9:19     韩天阳      v1.0.0      创建
 */
public class ConditionTest {
    private ReentrantLock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public static void main(String[] args) {
        ConditionTest thread10 = new ConditionTest();
        try {
            thread10.print();
            Thread.sleep(3000);
            thread10.signal();
        }
        catch (Exception e) {
        }
    }

    public void print() {
        new Thread(() -> {
            try {
                // 释放锁 同时当前线程阻塞
                lock.lock();
                System.out.println(Thread.currentThread().getName() + ",1");
                condition.await();
                System.out.println(Thread.currentThread().getName() + ",2");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }).start();
    }

    public void signal() {
        try {
            lock.lock();
            condition.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
